package com.hs.srm.admin.modules.bills.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.bills.dao.ScmBillsDao;
import com.hs.srm.admin.modules.bills.dao.ScmBillsHeaderDao;
import com.hs.srm.admin.modules.bills.entity.ScmBillsEntity;
import com.hs.srm.admin.modules.bills.entity.ScmBillsHeaderEntity;
import com.hs.srm.admin.modules.bills.service.ScmBillsHeaderService;
import com.hs.srm.admin.modules.orders.dao.ScmPoHeaderDao;
import com.hs.srm.admin.modules.orders.dao.ScmPoItemDao;
import com.hs.srm.admin.modules.orders.dao.StockDao;
import com.hs.srm.admin.modules.orders.entity.ScmPoItemEntity;
import com.hs.srm.admin.modules.orders.entity.StockEntity;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.base.modules.sys.shiro.ShiroUtils;
import com.hs.srm.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sunnykid.text.SunnykidText;
import sunnykid.util.ArrayUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对账单Service控制层实现类
 * <p>標題：</p>
 * <p>描述：</p>
 * <p>版權：版權所有 (c) 2019</p>
 * <p>組織：厚声集团</p>
 *
 * @author 鐘曉籟
 * @version V1.0.0
 * @date 2019年4月2日 上午9:46:29
 * @since V1.0.0
 */
@Service("scmBillsHeaderService")
public class ScmBillsHeaderServiceImpl extends ServiceImpl<ScmBillsHeaderDao, ScmBillsHeaderEntity> implements
		ScmBillsHeaderService {

	@Autowired
	private ScmBillsDao scmBillsDao;

	@Autowired
	private ScmBillsHeaderDao scmBillsHeaderDao;

	@Autowired
	private StockDao stockDao;

	@Autowired
	private ScmPoHeaderDao scmPoHeaderDao;

	@Autowired
	private ScmPoItemDao scmPoItemDao;

	@Override
	public PageUtils selectDetailPage(Map<String, Object> params) {
		Page<ScmBillsHeaderEntity> page = this.selectDetailPage(new QueryParam<ScmBillsHeaderEntity>(params).getPage(),
				new EntityWrapper<>(), params);
		return new PageUtils(page);
	}

	private Page<ScmBillsHeaderEntity> selectDetailPage(Page<ScmBillsHeaderEntity> page,
			Wrapper<ScmBillsHeaderEntity> wrapper, Map<String, Object> params) {
		page.setRecords(this.baseMapper.selectDetailPage(page, wrapper, params));
		return page;
	}

	@Override
	public int genBills(String[] poIds, String[] stockIds, String vendorCode, BigDecimal sumMoney) {
		String userName = ShiroUtils.getUserEntity().getUsername();
		Date createDate = new Date();
		ScmBillsHeaderEntity hes = new ScmBillsHeaderEntity();
		Map<String,Object> map =new HashMap<>();
		String billNo = this.generateBillNo();
		hes.setSumMoney(sumMoney);
		hes.setBillNo(billNo);
		hes.setPkId(SunnykidText.getUUID());
		hes.setCreateBy(userName);
		hes.setCreateDate(createDate);
		hes.setVendorId(vendorCode);
		List<ScmBillsEntity> list = this.scmBillsDao.findByStockId(map,stockIds);
		for(ScmBillsEntity bill : list) {
			bill.setBillNo(billNo);
			bill.setCreatePeople(userName);
			bill.setCreateTime(createDate);
			this.scmBillsDao.insert(bill);
		}
		this.scmBillsHeaderDao.insert(hes);
		this.stockDao.updateStockStatus(stockIds);
		return list.size();
	}

	/**
	 * 生成对账单号
	 *
	 * @return String
	 */
	private String generateBillNo() {
		return SunnykidText.getUUID();
	}

	@Override
	public BigDecimal calSumMoney(String[] stockIds) {
		BigDecimal sumMoney = BigDecimal.ZERO;
		List<StockEntity> stocks = this.stockDao.selectBatchIds(ArrayUtils.toList(stockIds));
		for(StockEntity stock : stocks) {
			String itemNo = stock.getItemNo();
			String poNo = stock.getPoNo();
			if(itemNo == null || poNo == null) {
				return new BigDecimal(-100);// -100表示入库单中没有订单信息或订单行项目信息
			}
			List<ScmPoItemEntity> itemList = this.scmPoItemDao.selectList(new EntityWrapper<ScmPoItemEntity>().eq(
					"po_no", poNo).eq("item_no", itemNo));
			if(itemList == null || itemList.isEmpty()) {
				return new BigDecimal(-200);// -200表示无法查找到订单行项目信息
			}
			BigDecimal count = stock.getShippedQty();// 数量
			BigDecimal price = itemList.get(0x0).getPoPrice();// 单价

			if(count == null) {
				return new BigDecimal(-300);// -300表示收货记录中没有数量信息
			}
			if(price == null) {
				return new BigDecimal(-400);// -400表示订单行项目中没有单价信息
			}
			BigDecimal itemTotalMoney = count.multiply(price);// 单个行项目的总价
			sumMoney = sumMoney.add(itemTotalMoney);// 累加
		}
		return sumMoney;
	}

}
